<?php

class Template 
{
	 
	 public static $SQL_PATH="sql";
	 public static $HTML_PATH="template";
	 
	  private static function baseDir()
	 {
		 return $_SERVER['DOCUMENT_ROOT']."/powerliftingunites/web/";
	 }
	 
	 public static function get_sql_query ($file)
	 {
		 $query=file_get_contents(self::baseDir().self::$SQL_PATH.'/'.$file);
		 return $query;
	 }
	 		 
	 public static function get_html_query ($file)
	 {
		 $html=file_get_contents(self::baseDir().self::$HTML_PATH.'/'.$file);
		 return $html;
	 }
	 
	 public static function render ($file, $type, $replace_arr)
	 {
		 if ($type=="sql")
			 $path=self::$SQL_PATH;
		 else 
			 $path=self::$HTML_PATH;	 
		 
		 $templ=file_get_contents(self::baseDir().$path.'/'.$file);
		 $templ_keys=self::keys($file, $path);		 
				 
		 for ($i=0;$i<count($replace_arr);$i++)
		 {
			 $str_pos=strpos($templ, "##$templ_keys[$i]##");
			 while($str_pos!==false)
			 {
				 $templ=preg_replace("/##$templ_keys[$i]##/", $replace_arr[$templ_keys[$i]], $templ); 
				 $str_pos=strpos($templ, "##$templ_keys[$i]##");
			 }
			 	
		 }
		 		 	
		 return $templ;	 		 
	 }
	 
	  public static function keys($file,$path)
	 {
		 		 
		 $templ=file_get_contents(self::baseDir().$path.'/'.$file);
		 preg_match_all("/##[a-zA-Z0-9_]{1,}##/",$templ, $array);
		 $array=$array[0];
		 
		 foreach($array as $k=>$v)
		 {
			 $array[$k]=preg_replace("/##/","",$v);	
		 }
		 
		return $array;
  	 }
	 

}

?>